/**
 * 数据库信息配置相关JS
 */

$(function () {
    // 初始化表格
    var dg = $('#dg').datagrid({
        url: basePath + "/rptTools/accessIn/queryAccessInSys",
        singleSelect: true,
        rownumbers:true,
        striped: true,
        remoteSort: true,
        pagination:true,//显示分页
        pageSize: 20,
        columns:[
            [
                {field:'sid',title:'系统ID',width:80},
                {field:'sysName',title:'系统名称',width:220},
                {field:'sysCode',title:'系统代码',width:220},
                {field:'sysToken',title:'系统Token',width:310},
                {field:'operate',title:'操作',width:300,align:'center',formatter:function(value,row,index){
                    var $row = JSON.stringify(row).replace(/\"/g,"'");
                    return '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="accessIn.editAccessInSys(' +  $row + ')" href="#">修改配置</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:10px;" onClick="accessIn.delAccessInSys(\'' +  row.sysName + '\',\'' + row.sysCode +'\',' + row.sid + ')" href="#">删除配置</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:35px;" onClick="accessIn.setToken(\'' +  row.sysCode + '\',' + row.sid + ')" href="#">设置Token</a>'
                         + '<a style="color:#4697e5; text-decoration:none;padding-left:10px;" onClick="accessIn.clearToken(\'' +  row.sysCode + '\',' + row.sid + ')" href="#">清除Token</a>';
                }}
            ]
        ]
    });
});

var accessIn = {
    /**
     * 查询数据
     */
    query : function() {
        var sysName = $('#sysName').val();  // 数据库名称

        var param = {};
        param.sysName = sysName;
        $('#dg').datagrid({
            url:basePath + "/rptTools/accessIn/queryAccessInSys",
            queryParams:param
        });
    },
    addAccessInSys : function () {
        this.resetWin();
        $('#addAccessInWin').window('open');

    },
    editAccessInSys : function (accessInSys) {
        this.resetEditWin();
        var sid = accessInSys.sid;
        var sysName = accessInSys.sysName;
        var sysCode = accessInSys.sysCode;
        $("#sid").val(sid);
        $("#editSysName").textbox('setValue',sysName);
        $("#editSysCode").textbox('setValue',sysCode);
        $('#editAccessInWin').window('open');
    },
    delAccessInSys : function (sysName, sysCode, sysId) {
        var confirmMsg = '确认删除' + sysName + '吗?'
        var func = this;
        $.messager.confirm('提示', confirmMsg, function(r){
            if (r){
                func.showMask();
                $.ajax({
                    url:basePath + "/rptTools/accessIn/deleteAccessIn",
                    type:'POST',
                    dataType:'json',
                    data:{
                        sid: sysId,
                        sysCode: sysCode
                    },
                    async: false,
                    success:function(data){
                        $("#mask").hide();
                        var status = data.status;
                        var errorMsg = data.errorMsg;
                        if(status == "1") {
                            alert("删除报表接入系统成功");
                            $('#dg').datagrid({
                                url:basePath + "/rptTools/accessIn/queryAccessInSys"
                            });
                        } else {
                            alert(errorMsg);
                        }
                    }
                })
            }
        });
    },
    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").show();
    },
    saveAccessInSys : function () {
        var errorMsg = '';
        var errorFlag = false;
        var sysName = $("#instSysName").val();
        var sysCode = $("#instSysCode").val();
        if(sysName == '') {
            errorMsg = '系统名称不能为空';
            errorFlag = true;
        } else if(sysCode == '') {
            errorMsg = '系统代码不能为空';
            errorFlag = true;
        } else {
            var reg =  /^[0-9a-zA-Z]*$/;
            if (!reg.test(sysCode)) {
                errorMsg = '系统代码只能由字母或数字组成';
                errorFlag = true;
            }
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.sysName = sysName;
        param.sysCode = sysCode;
        $.ajax({
            url:basePath + "/rptTools/accessIn/addAccessIn",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("新增报表接入系统成功");
                    $('#addAccessInWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/accessIn/queryAccessInSys"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    updateAccessSysIn : function () {
        var errorMsg = '';
        var errorFlag = false;
        var sysName = $("#editSysName").val();
        var sysCode = $("#editSysCode").val();
        var sid = $("#sid").val();
        if(sysName == '') {
            errorMsg = '系统名称不能为空';
            errorFlag = true;
        }else if(sysCode == '') {
            errorMsg = '系统代码不能为空';
            errorFlag = true;
        } else {
            var reg =  /^[0-9a-zA-Z]*$/;
            if (!reg.test(sysCode)) {
                errorMsg = '系统代码只能由字母或数字组成';
                errorFlag = true;
            }
        }
        if(errorFlag) {
            alert(errorMsg);
            return;
        }

        var param = {};
        param.sysName = sysName;
        param.sysCode = sysCode;
        param.sid = sid;
        $.ajax({
            url:basePath + "/rptTools/accessIn/updateAccessIn",
            type:'POST',
            dataType:'json',
            data: param,
            async: false,
            success:function(data){
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("报表接入系统修改成功");
                    $('#editAccessInWin').window('close');
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/accessIn/queryAccessInSys"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    setToken : function (sysCode, sysId) {
        this.showMask();
        var param = {};
        param.sid = sysId;
        param.sysCode = sysCode;
        $.ajax({
            url:basePath + "/rptTools/accessIn/setSysToken",
            type:'POST',
            dataType:'json',
            data: param,
            async: true,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("设置系统Token成功");
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/accessIn/queryAccessInSys"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    clearToken : function (sysCode, sysId) {
        this.showMask();
        var param = {};
        param.sid = sysId;
        param.sysCode = sysCode;

        $.ajax({
            url:basePath + "/rptTools/accessIn/clearSysToken",
            type:'POST',
            dataType:'json',
            data: param,
            async: true,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                var errorMsg = data.errorMsg;
                if(status == "1") {
                    alert("置空系统Token成功");
                    $('#dg').datagrid({
                        url:basePath + "/rptTools/accessIn/queryAccessInSys"
                    });
                } else {
                    alert(errorMsg);
                }
            }
        })
    },
    resetWin : function () {
        $("#instSysName").textbox('setValue','');
        $("#instSysCode").textbox('setValue','');
    },
    resetEditWin : function () {
        $("#editSysName").textbox('setValue','');
        $("#editSysCode").textbox('setValue','');
    },
    closeWin : function (winId) {
        $('#' + winId).window('close');
    }
}
